package com.karst.essentials.chapter04
{
	import flash.events.Event;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	import org.aswing.AsWingManager;
	import org.papervision3d.materials.VideoStreamMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
	
	internal class VideoMaterialExample extends BasicView
	{
		public function VideoMaterialExample()
		{
			super() ;
			AsWingManager.getStage().frameRate = 12 ;
			createView() ;
			startRendering() ;
		}
		
		private var conn : NetConnection = null ;
		private var video : Video = null ;
		private var stream : NetStream = null ;
		private var material : VideoStreamMaterial = null ;
		private var plane : Plane = null ;
		private function createView() : void
		{
			if( video == null )
			{
				video = new Video( 600, 400 ) ;  //声明并创建一个视频。宽，高
				video.attachCamera( Camera.getCamera() ) ; //视频. 附上摄像头（摄像头. 获取摄像头）
			}
			
			if( conn == null )
				conn = new NetConnection() ;
			conn.connect( null ) ; //如果连接到一个本地视频文件，此参数可设为null
			
			if( stream == null )
				stream = new NetStream( conn ) ; //声明并创建一个网络数据流，调用一个网络连接

			if( material == null )
			{
				//声明并创建视频流材质，调用视频对象、网络数据流
				material = new VideoStreamMaterial( video, stream, true, true ) ;
				material.animated = true; //将视频流材质的动画属性设为true.
			}
			
			if( plane == null )
				plane = new Plane( material, 600, 400, 2, 2 ) ;
			
			scene.addChild( plane ) ;
			camera.z = -800 ;
		}
		
		override protected function onRenderTick(event:Event=null):void
		{
			super.onRenderTick() ;
		}
	}
}